﻿using UnityEngine;

[DisallowMultipleComponent]
public class CameraFakeObj : MonoBehaviour
{
    public Transform m_TopLeft;
    public Transform m_BottomRight;
    public float m_RotateSpeed = 5;

    public UIViewport Viewport { get; private set; }
    public Transform ModelTrans { get; private set; }

    void Awake()
    {
        GameObject go = GameObject.Find("ModelShow");
        if (go != null)
        {
            Viewport = go.GetComponentInChildren<UIViewport>();
        }
        ShowFakeObj(false); Init();
    }

    void OnDrag(Vector2 delta)
    {
        if (ModelTrans == null) { return; }
        if (delta.x > 0)
        {
            ModelTrans.Rotate(Vector3.up * -m_RotateSpeed, Space.Self);
        }
        else
        {
            ModelTrans.Rotate(Vector3.up * m_RotateSpeed, Space.Self);
        }
    }

    public void ShowFakeObj(bool show)
    {
        if(Viewport != null)
        {
            Utility.SetActive(Viewport, show);
        }
    }

    public void Init()
    {
        if (Viewport == null) { return; }
        if (m_TopLeft && m_BottomRight)
        {
            Viewport.topLeft = m_TopLeft;
            Viewport.bottomRight = m_BottomRight;
        }
        if (UICamera.mainCamera)
        {
            Viewport.sourceCamera = UICamera.mainCamera;
        }
    }
}